<?php


namespace app\admin\controller;


use app\common\lib\Status;

class Goods extends AdminBase
{

    //Goods分页 搜索
    public function index()
    {
        $data = [];
        $title = input("param.title", "", "trim");
        $time = input("param.time", "", "trim");


        if (!empty($title)) {
            $data['title'] = $title;
        }
        if (!empty($time)) {
            $data['create_time'] = explode(" - ", $time);
        }

        //分页逻辑
        $goods = (new \app\common\business\Goods())->getLists($data, "5");


        return view("", [
            "goods" => $goods,
            "title"=>$title,
            "time"=>$time,

        ]);
    }

    public function add()
    {
        return view();
    }

    //新增逻辑
    public function save()
    {
        if (!$this->request->isPost()) {
            return show(config('status.error'), "请求不合法");
        }
        //校验
        $data = input("param.");


        //token 校验
//        $check = $this->request->checkToken('__token__');
//        if (!$check) {
//            return show(config('status.error'), '无效请求');
//        }
//        // halt($data);
//        $validate = new \app\admin\validate\Goods();
//        if (!$validate->scene('save')->check($data)) {
//            return show(config('status.error'), $validate->getError());
//        }

        //数据处理
        $data['category_path_id'] = $data['category_id'];
        $result = explode(",", $data['category_path_id']); //转换成数组
        $data['category_id'] = end($result); //获取最后一个id
        //  halt($data);

        //新增商品逻辑
        $res = (new \app\common\business\Goods())->insertData($data);
        if (!$res) {
            return show(config('status.error'), '新增失败');
        }
        return show(config('status.success'), '新增成功');
    }

    /**
     * 排序
     * @return \think\response\Json
     */
    public function listorder()
    {
        //获取数据
        $id = input('id');
        $listorder = input('listorder');


        //验证
        $data = [
            'id' => $id,
            'listorder' => $listorder,
        ];
        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('listorder')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        //排序逻辑
        try {
            $res = (new \app\common\business\Goods())->listorder($id, $listorder);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }


        if (!empty($res)) {
            return show(config('status.success'), '排序成功');
        } else {
            return show(config('status.error'), '排序失败');
        }

    }

    /**
     * 更新状态
     * @return \think\response\Json
     */
    public function status()
    {

        //接收数据
        $status = input("param.status", 0, "intval");
        $id = input("param.id", 0, "intval");

        //验证
        $data = [
            'status' => $status,
            'id' => $id,
        ];
        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('status')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        //如果id不存在  或者 状态码里 没这四个就是错误参数
        if (!$id || !in_array($status, Status::getTableStatus())) {
            return show(config('status.error'), '参数错误');
        }

        //更新状态逻辑
        try {
            $res = (new \app\common\business\Goods())->status($id, $status);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return show(config('status.success'), '更新状态成功');
        } else {
            return show(config('status.error', '更新状态失败'));
        }

    }

    /**
     * 是否推荐
     * @return \think\response\Json
     */
    public function isIndexRecommend()
    {
        //接收数据
        $index = input("param.is_index_recommend", 0, "intval");
        $id = input("param.id", 0, "intval");

        //验证
        $data = [
            'is_index_recommend' => $index,
            'id' => $id,
        ];
        $validate = new \app\admin\validate\Goods();
        if (!$validate->scene('index')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        //是否推荐逻辑
        try {
            $res = (new \app\common\business\Goods())->isIndexRecommend($id, $index);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if ($res) {
            return show(config('status.success'), '更新状态成功');
        } else {
            return show(config('status.error', '更新状态失败'));
        }

    }


}